package com.whity.towerdefense.model;

import java.util.LinkedList;
import java.util.List;

import com.whity.towerdefense.view.GraphicElt;
import com.whity.towerdefense.view.GraphicEltFactory;

import android.graphics.Point;
import android.util.Log;

public class Cell
{
	// =============================================================
	// Public attributs :
	
	// Position of the cell [0;0] for the top left, [1;0] for the one on it right
	public final Point				m_Position;

	// =============================================================
	// Private attributs :
	
	@SuppressWarnings("unused")
	private static final String		TAG = "com.djm.test03.model";
	
	private final CellType 			m_CellType;
	private GraphicElt				m_GraphicElt;
	private List<Cell>				m_Neighbours;
	private Tower					m_Tower;
	private List<Unit>				m_Units;
	
	// =============================================================
	// Public functions :
	
	public Cell(CellType p_CellType, Point p_Position)
	{
		m_CellType = p_CellType;
		m_Position = p_Position;
		m_GraphicElt = GraphicEltFactory.CreateGraphicElt("Maps/Cell01");
		m_Neighbours = new LinkedList<Cell>();
		m_Units = new LinkedList<Unit>();
		m_Tower = null;
	}
	
	public Cell(CellType p_CellType, int p_X, int p_Y)
	{
		this(p_CellType, new Point(p_X, p_Y));
	}
	
	public boolean IsWalkable()
	{
		return m_CellType.m_Walkable;
	}
	
	public boolean IsBuildable()
	{
		return m_CellType.m_Buildable;
	}
	
	public boolean IsSpawnable()
	{
		return m_CellType.m_Spawnable;
	}
	
	public GraphicElt GetGraphic()
	{
		return m_GraphicElt;
	}
	
	public void AddNeighbour(Cell p_Neighbour)
	{
		m_Neighbours.add(p_Neighbour);
	}
	
	public List<Cell> GetNeighbour()
	{
		return m_Neighbours;
	}
	
	public void SetTower(Tower p_Tower)
	{
		if(m_Tower != null)
		{
			Log.w(TAG, "Tryto put 2 tower on the same cell ! Removing the older one. Cell(" + m_Position.x + " ; " + m_Position.y + ")" );
		}
		m_Tower = p_Tower;
	}
	
	public float CostToCross()
	{
		float res = 1.0f;
		if(m_Tower != null)
		{
			res += 1000.0f;
		}
		return res;
	}
	
	public List<Unit> GetUnits()
	{
		return m_Units;
	}
	
	public void SetUnit(Unit p_Unit)
	{
		m_Units.add(p_Unit);
	}
	
	public void RemoveUnit(Unit p_Unit)
	{
		m_Units.remove(p_Unit);
	}
	
	// =============================================================
}
